package com.yvan.codeslave.action;

import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.vfs.VirtualFileManager;
import com.intellij.util.ExceptionUtil;
import com.yvan.codeslave.PluginUtils;
import com.yvan.codeslave.batch.CodeSlave;
import com.yvan.codeslave.snippet.Idea;
import com.yvan.platform.CodeSlaveSetting;
import lombok.val;

public class BuildCodeAction extends AnAction {

    public BuildCodeAction() {
        super("生成代码");
    }

    @Override
    public void actionPerformed(AnActionEvent e) {

        String ret = "";
        try {

            val state = PluginUtils.getState();
            CodeSlave.INSTANCE.codeSlaveSetting = new CodeSlaveSetting(state.basePath);
            CodeSlave.INSTANCE.reload();

            ret = CodeSlave.INSTANCE.run(
                    e.getProject().getBasePath(),
                    PluginUtils.moduleFile.getVirtualFile().getPath());

        } catch (Exception ex) {
            ExceptionUtil.rethrow(ex);
        }

        Idea.notifyInfo("生成成功:\r\n" + ret);

        // 同步刷新
        VirtualFileManager.getInstance().syncRefresh();
    }
}
